package com.usian.controller;

import com.usian.feign.SSOServiceFeign;
import com.usian.pojo.TbUser;
import com.usian.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 用户注册与登录
 */
@RestController
@RequestMapping("/frontend/sso")
public class SSOController {

    @Autowired
    private SSOServiceFeign ssoSeriviceFeign;

    @RequestMapping("checkUserInfo/{checkValue}/{checkFlag}")
    public Result checkUserInfo(@PathVariable String checkValue,@PathVariable Integer checkFlag){
       Boolean b = ssoSeriviceFeign.checkUserInfo(checkValue,checkFlag);
       if (b){
           return Result.ok(b);
       }
           return Result.error("校验失败");
    }

    /**
     * 用户注册
     */
    @RequestMapping("/userRegister")
    public Result userRegister(TbUser user){
        Integer userRegister = ssoSeriviceFeign.userRegister(user);
        if (userRegister == 1){
            return Result.ok();
        }
            return Result.error("注册失败");
    }

    /**
     * 用户登录
     */
    @RequestMapping("/userLogin")
    public Result userLogin(String username, String password){
        Map map = ssoSeriviceFeign.userLogin(username,password);
        if (map!=null){
            return Result.ok(map);
        }
            return Result.error("登录失败");
    }

    /**
     * 查询用户登录是否过期
     */
    @RequestMapping("/getUserByToken/{token}")
    @ResponseBody
    public Result getUserByToken(@PathVariable String token){
    TbUser tbUser =  ssoSeriviceFeign.getUserByToken(token);
    if (tbUser!=null){
        return Result.ok(tbUser);
    }
        return Result.error("登录过期");
    }

    /**
     * 用户退出登录
     */
    @RequestMapping("/logOut")
    public Result logOut(String token){
        Boolean logOut = ssoSeriviceFeign.logOut(token);
        if (logOut){
            return Result.ok(true);
        }
            return Result.error("退出失败");
    }
}